package com.uuuo.retrofit2;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.uuuo.retrofit2.Converter.BaseSubscriber;
import com.uuuo.retrofit2.Converter.HttpResult;
import com.uuuo.retrofit2.Entity.ArticleList;
import com.uuuo.retrofit2.Entity.BannerEntity;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Context context;
    private Retrofit retrofit;
    private WanAndroidApi retrofitAPI;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .build();


        retrofit = new Retrofit.Builder()
                .baseUrl("http://www.wanandroid.com/")
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        retrofitAPI = retrofit.create(WanAndroidApi.class);
    }

    public void get(View view) {

//        Call<HttpResult<List<BannerEntity>>> bannerList = retrofitAPI.getBannerList();
//        bannerList.enqueue(new Callback<HttpResult<List<BannerEntity>>>() {
//            @Override
//            public void onResponse(Call<HttpResult<List<BannerEntity>>> call, Response<HttpResult<List<BannerEntity>>> response) {
//                HttpResult<List<BannerEntity>> list = response.body();
//
//                for (BannerEntity datum : list.data) {
//                    Log.i("11111",datum.toString());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<HttpResult<List<BannerEntity>>> call, Throwable t) {
//
//            }
//        });
        retrofitAPI.getBannerList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<HttpResult<List<BannerEntity>>>(context) {
                    @Override
                    public void onNext(HttpResult<List<BannerEntity>> listHttpResult) {
                        super.onNext(listHttpResult);

                        for (BannerEntity bannerEntity : listHttpResult.data) {
                            Log.i(TAG, bannerEntity.toString());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    public void artList(View view) {

        //Observable<HttpResult<ArticleList>> getProjectList();
//        retrofitAPI.getProjectList(2,294)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new BaseSubscriber<HttpResult<ArticleList>>(context) {
//                    @Override
//                    public void onNext(HttpResult<ArticleList> articleListHttpResult) {
//                        super.onNext(articleListHttpResult);
//
//                        int count = articleListHttpResult.data.getPageCount();
//                        Log.i(TAG, count + "");
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        super.onError(e);
//                    }
//                });

        retrofitAPI.getProjectList(2, 294)
                .compose(BaseSubscriber.<ArticleList>applySchedulers())
                .subscribe(new BaseSubscriber<ArticleList>(context) {

                    @Override
                    public void onNext(ArticleList articleList) {
                        super.onNext(articleList);
                        int count =articleList.getDatas().size();
                        Log.i(TAG, count + "");
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }
}
